Skip to content

[区块链]2 以太坊 geth dev模式、交互console、有交易挖矿、自动挖矿

前言

上节我们讲了基于ubuntu geth搭建以太坊私有链,并以dev方式启动。什么是dev模式?有什么特点本节我们探讨。

dev 模式

dev 模式,也叫回归测试模式,主要用来给开发人员提供一个方便的开发测试环境。

在dev模式下,可以轻松的获得以太币,方便发起交易,交易也会被快速的打包,节省时间方便验证。

先来看看dev 启动命令

bash
geth --networkid 15 --dev --rpc --rpcapi "db,eth,net,web3,miner,personal" console 2>>log

查看启动log,在 dev 模式下,启动节点后,系统默认提供一个开发者账号:address=0xD7Ec36444D13Cc079eB116B4f2602CFfCDc9aDEE,这个账号会作为当前的 coinbase 账号,在 keystore 目录下也有对应的加密私钥文件。

bash

Sanitizing cache to Go's GC limits       provided=1024 updated=656

INFO [10-18|10:56:44.569] Maximum peer count                       ETH=25 LES=0 total=25

INFO [10-18|10:56:47.682] Using developer account                  address=0xD7Ec36444D13Cc079eB116B4f2602CFfCDc9aDEE

INFO [10-18|10:56:47.682] Starting peer-to-peer node               instance=Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10

INFO [10-18|10:56:47.682] Writing custom genesis block 

INFO [10-18|10:56:47.684] Persisted trie from memory database      nodes=12 size=1.79kB time=61.278µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

INFO [10-18|10:56:47.684] Initialised chain configuration          config="{ChainID: 1337 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: <nil> Engine: clique}"

INFO [10-18|10:56:47.685] Initialising Ethereum protocol           versions="[63 62]" network=15

INFO [10-18|10:56:47.685] Loaded most recent local header          number=0 hash=478c38…cd8659 td=1 age=49y6mo2d

INFO [10-18|10:56:47.685] Loaded most recent local full block      number=0 hash=478c38…cd8659 td=1 age=49y6mo2d

INFO [10-18|10:56:47.685] Loaded most recent local fast block      number=0 hash=478c38…cd8659 td=1 age=49y6mo2d

INFO [10-18|10:56:47.686] Stored checkpoint snapshot to disk       number=0 hash=478c38…cd8659

INFO [10-18|10:56:47.685] Starting P2P networking 

INFO [10-18|10:56:47.686] started whisper v.6.0 

INFO [10-18|10:56:47.688] IPC endpoint opened                      url=/tmp/geth.ipc

INFO [10-18|10:56:47.689] HTTP endpoint opened                     url=http://127.0.0.1:8545 cors= vhosts=localhost

INFO [10-18|10:56:47.689] Transaction pool price threshold updated price=1000000000

INFO [10-18|10:56:47.689] Transaction pool price threshold updated price=1

INFO [10-18|10:56:47.689] Etherbase automatically configured       address=0xD7Ec36444D13Cc079eB116B4f2602CFfCDc9aDEE

INFO [10-18|10:56:47.690] Commit new mining work                   number=1 sealhash=c1009a…5452e3 uncles=0 txs=0 gas=0 fees=0 elapsed=120.442µs

INFO [10-18|10:56:47.690] Sealing paused, waiting for transactions

交互console

执行上述 geth启动明后,会停留在一个交互界面上.

可以在> 后输入支持的命令 如eth.blockNumber 查看去看高度.

dev挖矿

dev启动有一些令人迷惑的地方,在 dev 默认模式启动的情况下,已经开启了挖矿,因此当再执行挖矿命令时会返回 null。也就是很多朋友问的为何执行 miner.start() 会返回 null。

bash

> miner.start()

null

查看挖矿命令的源码,发现无论是否成功挖矿都会返回 null。

solidity
func (s *Ethereum) StartMining(local bool) error {

    eb, err := s.Etherbase()

    if err != nil {

        log.Error("Cannot start mining without etherbase", "err", err)

        return fmt.Errorf("etherbase missing: %v", err)

    }

    if clique, ok := s.engine.(*clique.Clique); ok {

        wallet, err := s.accountManager.Find(accounts.Account{Address: eb})

        if wallet == nil || err != nil {

            log.Error("Etherbase account unavailable locally", "err", err)

            return fmt.Errorf("signer missing: %v", err)

        }

        clique.Authorize(eb, wallet.SignHash)

    }

    if local {

        // If local (CPU) mining is started, we can disable the transaction rejection

        // mechanism introduced to speed sync times. CPU mining on mainnet is ludicrous

        // so noone will ever hit this path, whereas marking sync done on CPU mining

        // will ensure that private networks work in single miner mode too.

        atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)

    }

    go s.miner.Start(eb)

    return nil

}

大家注意,log中最后一条

bash
INFO [10-18|10:56:47.690] Sealing paused, waiting for transactions

这就是默认启动 dev 模式的一个特性。geth 节点的开发者为了给测试环境提供一个更友好的操作:只有发过来交易,系统才会挖矿打包,如果未发送交易过来,就不会去挖矿打包。这样不用被一些并没有交易的区块刷屏了。

但是此模式有一个弊端,我们知道发送一笔交易想让它被确认多次才算成功,如果没交易不挖矿岂不是确认过程很费劲,还需要再在后面发送 N 次交易?这个不用担心,后面会讲到另外一种模式。

dev 之自动挖矿

上述启动是在有交易才挖矿,我们通过改变参数,可以自动挖矿。此参数默认为 0,也就是上面讲的被动挖矿的模式,当有 pending 交易到来才进行挖矿,同时它还有一个参数值 1,主动挖矿。

--dev.period value 开发者模式下挖矿周期 (0 = 有pending状态交易时进行挖矿) (默认: 0)

此时启动命令如下:

bash
geth --networkid 15 --dev --dev.period 1 --rpc --rpcapi "db,eth,net,web3,miner,personal"   console 2>>log

这样就回到常见的自动挖矿的模式了,我们可以执行 stop 命令来停止挖矿。


作者:码中自有黄金屋

来源:CSDN

原文:https://blog.csdn.net/billwzf/article/details/83145111

版权声明:本文为博主原创文章,转载请附上博文链接!

520608.com 备案号:粤ICP备13053123号